﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using SIPO.Library;
using SIPO.Library.DomainObject.Administration.Catalog;
using Assesment.Library.Enum;
using Assesment.Library.Interfaces;

namespace Assesment.Library.DataAccess.Administration.Catalog
{
    public partial class LevelCompetencyRepo : IRepository<LevelCompetency, int>
    {
        private string connectionString;
        public LevelCompetencyRepo(string ConnectionString)
        {
            connectionString = ConnectionString;
        }
        public object SelectScalar(SQL.Function.Aggregate function, string column)
        {
            object _result = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    StringBuilder sbQuery = new StringBuilder();
                    switch (function)
                    {
                        case SQL.Function.Aggregate.Max:
                            sbQuery.AppendFormat("SELECT MAX({0}) FROM master.LevelCompetency", column);
                            break;
                        case SQL.Function.Aggregate.Min:
                            sbQuery.AppendFormat("SELECT MIN({0}) FROM master.LevelCompetency", column);
                            break;
                        case SQL.Function.Aggregate.Distinct:
                            sbQuery.AppendFormat("SELECT DISTINCT({0}) FROM master.LevelCompetency", column);
                            break;
                        case SQL.Function.Aggregate.Count:
                            sbQuery.AppendFormat("SELECT COUNT({0}) FROM master.LevelCompetency", column);
                            break;
                        case SQL.Function.Aggregate.Sum:
                            sbQuery.AppendFormat("SELECT SUM({0}) FROM master.LevelCompetency", column);
                            break;
                        case SQL.Function.Aggregate.Avg:
                            sbQuery.AppendFormat("SELECT AVG({0}) FROM master.LevelCompetency", column);
                            break;
                        default:
                            // do nothing 
                            break;
                    }

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        _result = cmd.ExecuteScalar();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return _result;
        }
        public List<LevelCompetency> GetList()
        {
            List<LevelCompetency> tt = new List<LevelCompetency>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ID, CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy FROM master.LevelCompetency";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_SelectAll";
                    cmd.CommandType = CommandType.StoredProcedure;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                LevelCompetency t = new LevelCompetency();
                                t.ID = dr[LevelCompetency.Fields.ID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.ID].ToString());
                                t.CompetencyID = dr[LevelCompetency.Fields.CompetencyID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.CompetencyID].ToString());
                                t.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                t.Definition = dr[LevelCompetency.Fields.Definition] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.Definition].ToString();
                                t.CreatedBy = dr[LevelCompetency.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[LevelCompetency.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.CreatedDate].ToString());
                                t.UpdatedDate = dr[LevelCompetency.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[LevelCompetency.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public List<LevelCompetency> GetList(int pageSize, int currentPage, string sortName, string sortOrder)
        {
            List<LevelCompetency> tt = new List<LevelCompetency>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    int startRow = ((currentPage - 1) * pageSize) + 1;
                    int endRow = (startRow + pageSize);

                    StringBuilder sbQuery = new StringBuilder();
                    sbQuery.AppendFormat(" WITH result_set AS ");
                    sbQuery.AppendFormat(" ( ");
                    sbQuery.AppendFormat("    SELECT ");
                    sbQuery.AppendFormat("      ROW_NUMBER() OVER (ORDER BY {0} {1}) AS [row_number], ", sortName, sortOrder);
                    sbQuery.AppendFormat("      ID, CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy ");
                    sbQuery.AppendFormat("    FROM ");
                    sbQuery.AppendFormat("      master.LevelCompetency ");
                    sbQuery.AppendFormat(" ) ");
                    sbQuery.AppendFormat(" SELECT * FROM result_set WHERE [row_number] BETWEEN {0} AND {1} ", startRow, endRow);

                    cmd.CommandText = sbQuery.ToString();
                    cmd.CommandType = CommandType.Text;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                LevelCompetency t = new LevelCompetency();
                                t.ID = dr[LevelCompetency.Fields.ID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.ID].ToString());
                                t.CompetencyID = dr[LevelCompetency.Fields.CompetencyID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.CompetencyID].ToString());
                                t.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                t.Definition = dr[LevelCompetency.Fields.Definition] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.Definition].ToString();
                                t.CreatedBy = dr[LevelCompetency.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[LevelCompetency.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.CreatedDate].ToString());
                                t.UpdatedDate = dr[LevelCompetency.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[LevelCompetency.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.UpdatedBy].ToString();
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }
        public LevelCompetency GetById(int key)
        {
            LevelCompetency t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ID, CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy FROM master.LevelCompetency WHERE ID = @ID";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_SelectByID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = key;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new LevelCompetency();
                                t.ID = dr[LevelCompetency.Fields.ID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.ID].ToString());
                                t.CompetencyID = dr[LevelCompetency.Fields.CompetencyID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.CompetencyID].ToString());
                                t.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                t.Definition = dr[LevelCompetency.Fields.Definition] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.Definition].ToString();
                                t.CreatedBy = dr[LevelCompetency.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[LevelCompetency.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.CreatedDate].ToString());
                                t.UpdatedDate = dr[LevelCompetency.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.UpdatedDate].ToString());
                                t.UpdatedBy = dr[LevelCompetency.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.UpdatedBy].ToString();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }
        public bool Save(LevelCompetency domain)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "INSERT INTO master.LevelCompetency(CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy) VALUES(@CompetencyID, @Level, @Definition, @CreatedBy, @CreatedDate, @UpdatedDate, @UpdatedBy)";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_Insert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = domain.CompetencyID;
                    cmd.Parameters.Add("@Level", SqlDbType.SmallInt).Value = (object)domain.Level ?? DBNull.Value;
                    cmd.Parameters.Add("@Definition", SqlDbType.Text).Value = (object)domain.Definition ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.CreatedBy ?? DBNull.Value;
                    cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.CreatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }

                }
            }
            return result;
        }
        public bool Update(LevelCompetency domain)
        {
            int result = 0;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "UPDATE master.LevelCompetency SET CompetencyID = @CompetencyID, Level = @Level, Definition = @Definition, UpdatedDate = @UpdatedDate, UpdatedBy = @UpdatedBy WHERE ID = @ID";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_Update";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = domain.ID;
                    cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = domain.CompetencyID;
                    cmd.Parameters.Add("@Level", SqlDbType.SmallInt).Value = (object)domain.Level ?? DBNull.Value;
                    cmd.Parameters.Add("@Definition", SqlDbType.Text).Value = (object)domain.Definition ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                    cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                    try
                    {
                        conn.Open();
                        result = cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return (result > 0);
        }
        public bool Delete(int key)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "DELETE FROM master.LevelCompetency WHERE ID = @ID";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_Delete";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@ID", SqlDbType.Int).Value = key;

                    try
                    {
                        conn.Open();
                        cmd.ExecuteNonQuery();
                        result = true;
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return result;
        }

        public bool Save(List<LevelCompetency> domains)
        {
            bool result = false;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                conn.Open();
                SqlTransaction txn = conn.BeginTransaction();
                try
                {
                    domains.ForEach(delegate(LevelCompetency domain)
                    {
                        SqlCommand cmd = conn.CreateCommand();
                        cmd.CommandText = "SP_LevelCompetency_Insert";
                        cmd.CommandType = CommandType.StoredProcedure;
                        //cmd.CommandText = "INSERT INTO master.LevelCompetency(CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy) VALUES(@CompetencyID, @Level, @Definition, @CreatedBy, @CreatedDate, @UpdatedDate, @UpdatedBy)";
                        //cmd.CommandType = CommandType.Text;
                        cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = domain.CompetencyID;
                        cmd.Parameters.Add("@Level", SqlDbType.SmallInt).Value = (object)domain.Level ?? DBNull.Value;
                        cmd.Parameters.Add("@Definition", SqlDbType.Text).Value = (object)domain.Definition ?? DBNull.Value;
                        cmd.Parameters.Add("@CreatedBy", SqlDbType.VarChar).Value = (object)domain.CreatedBy ?? DBNull.Value;
                        cmd.Parameters.Add("@CreatedDate", SqlDbType.DateTime).Value = (object)domain.CreatedDate ?? DBNull.Value;
                        cmd.Parameters.Add("@UpdatedDate", SqlDbType.DateTime).Value = (object)domain.UpdatedDate ?? DBNull.Value;
                        cmd.Parameters.Add("@UpdatedBy", SqlDbType.VarChar).Value = (object)domain.UpdatedBy ?? DBNull.Value;

                        cmd.Transaction = txn;
                        cmd.ExecuteNonQuery();
                        cmd.Dispose();
                    });

                    result = true;
                    txn.Commit();
                }
                catch (Exception ex)
                {
                    txn.Rollback();
                    throw (ex);
                }
                finally
                {
                    if (conn.State == ConnectionState.Open) conn.Close();
                }


            }
            return result;
        }

        public LevelCompetency GetByCompetencyID(int CompetencyID)
        {
            LevelCompetency t = null;
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ID, CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy FROM master.LevelCompetency WHERE CompetencyID = @CompetencyID";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_SelectBy_CompetencyID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = CompetencyID;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                t = new LevelCompetency();
                                t.ID = dr[LevelCompetency.Fields.ID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.ID].ToString());
                                t.CompetencyID = dr[LevelCompetency.Fields.CompetencyID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.CompetencyID].ToString());
                                t.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                t.Definition = dr[LevelCompetency.Fields.Definition] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.Definition].ToString();
                                t.CreatedBy = dr[LevelCompetency.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[LevelCompetency.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[LevelCompetency.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[LevelCompetency.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.UpdatedDate].ToString());

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return t;
        }

        public List<LevelCompetency> GetListByCompetencyID(int CompetencyID)
        {
            List<LevelCompetency> tt = new List<LevelCompetency>();
            using (SqlConnection conn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    //cmd.CommandText = "SELECT ID, CompetencyID, Level, Definition, CreatedBy, CreatedDate, UpdatedDate, UpdatedBy FROM master.LevelCompetency WHERE CompetencyID = @CompetencyID ORDER BY Level DESC";
                    //cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SP_LevelCompetency_SelectBy_CompetencyID";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.Add("@CompetencyID", SqlDbType.Int).Value = CompetencyID;
                    try
                    {
                        conn.Open();
                        using (SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                        {
                            while (dr.Read())
                            {
                                LevelCompetency t = new LevelCompetency();
                                t.ID = dr[LevelCompetency.Fields.ID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.ID].ToString());
                                t.CompetencyID = dr[LevelCompetency.Fields.CompetencyID] == DBNull.Value ? 0 : int.Parse(dr[LevelCompetency.Fields.CompetencyID].ToString());
                                t.Level = dr[LevelCompetency.Fields.Level] == DBNull.Value ? (short?)null : short.Parse(dr[LevelCompetency.Fields.Level].ToString());
                                t.Definition = dr[LevelCompetency.Fields.Definition] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.Definition].ToString();
                                t.CreatedBy = dr[LevelCompetency.Fields.CreatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.CreatedBy].ToString();
                                t.CreatedDate = dr[LevelCompetency.Fields.CreatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.CreatedDate].ToString());
                                t.UpdatedBy = dr[LevelCompetency.Fields.UpdatedBy] == DBNull.Value ? string.Empty : dr[LevelCompetency.Fields.UpdatedBy].ToString();
                                t.UpdatedDate = dr[LevelCompetency.Fields.UpdatedDate] == DBNull.Value ? (DateTime?)null : DateTime.Parse(dr[LevelCompetency.Fields.UpdatedDate].ToString());
                                tt.Add(t);
                                t.Dispose();
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        throw (ex);
                    }
                    finally
                    {
                        if (conn.State == ConnectionState.Open) conn.Close();
                    }
                }
            }
            return tt;
        }		
    }

}
